///////////////////////////////////////////////////////////////////////
// Reource names
///////////////////////////////////////////////////////////////////////
var res_names = new Object();
res_names['Food'] = '1';
res_names['Energy'] = '2';
res_names['Water'] = '3';
res_names['Animal embryos'] = '4';
res_names['Ore'] = '5';
res_names['Metal'] = '6';
res_names['Electronics'] = '7';
res_names['Robots'] = '8';
res_names['Heavy plastics'] = '9';
res_names['Hand weapons'] = '10';
res_names['Medicines'] = '11';
res_names['Nebula gas'] = '12';
res_names['Chemical supplies'] = '13';
res_names['Gem stones'] = '14';
res_names['Liquor'] = '15';
res_names['Hydrogen fuel'] = '16';
res_names['Exotic matter'] = '17';
res_names['Optical components'] = '18';
res_names['Radioactive cells'] = '19';
res_names['Droid modules'] = '20';
res_names['Bio-waste'] = '21';
res_names['Leech baby'] = '22';
res_names['Nutrient clods'] = '23';
res_names['Cybernetic X-993 Parts'] = '24';
res_names['X-993 Repair-Drone'] = '25';
res_names['Neural Stimulator'] = '26';
res_names['Battleweapon Parts'] = '27';
res_names['Slaves'] = '50';
res_names['Drugs'] = '51';
res_names['Package'] = '100';
res_names['Faction package'] = '101';
res_names['Explosives'] = '102';
res_names['VIP'] = '103';
res_names['Human intestines'] = '201';
res_names['Skaari limbs'] = '202';
res_names['Keldon brains'] = '203';
res_names['Rashkir bones'] = '204';
res_names['Exotic Crystal'] = '150';
res_names['Christmas Glitter'] = '104';
res_names['Military Explosives'] = '105';
res_names['Blue Sapphire jewels'] = '211';
res_names['Ruby jewels'] = '212';
res_names['Golden Beryl jewels'] = '213';
res_names['Neural Tissue'] = '28';
res_names['Stim Chip'] = '29';
///////////////////////////////////////////////////////////////////////
// Reource names
///////////////////////////////////////////////////////////////////////

function FLogProcessOwnBuildings () {
    var server = top.FLogGetServer ();
    var links = document.getElementsByTagName ("a");
    for (var i=0; i<links.length; i++) {
        var link = links[i];
        var href = link.getAttribute ("href");
        if (href.indexOf ("building_trade_settings.php") == -1) continue;
        var tileId = href.split ("=")[1];
        
        FLogProcessOwnBuilding (server, tileId, link.parentNode.parentNode, link.textContent);
    }
}

function FLogProcessOwnBuilding (server, tileId, row, btype) {
    var facility = server.GetBuilding (tileId, "facility");
    var defense = {};
    var stock = server.GetBuilding (tileId, "stock");
    var update = false;
    
    // Find the location
    var loc = row.cells[1].textContent;
    var locs = loc.split(" ");
    var coords = "[" + locs[locs.length-1] + "]";
    var sector = loc.substr (0, loc.length - coords.length);
    if (facility["sector"] != sector) {
        facility["sector"] = sector;
        update = true;
    }
    if (facility["coords"] != coords) {
        facility["coords"] = coords;
        update = true;
    }
    if (facility["building_type"] != btype) {
        facility["building_type"] = btype;
        update = true;
    }
    var owner = POgetUserName ();
    if (facility["owner"] != owner) {
        facility["owner"] = owner;
        update = true;
    }
    
    
    // Find the condition
    var cond = row.cells[3].textContent;
    if (facility["building_condition"] != cond) {
        facility["building_condition"] = cond;
        update = true;
    }
    
    // Find the defense modules
    
    // Save facility if needed
    if (update) {
        server.SaveFacility (tileId, facility, "facility");
    }
    
    // Find free space
    var space = row.cells[2].textContent.split("(")[1].split("}")[0];
    if (stock["space"] != space) {
        stock["space"] = space;
    }
    
    // Find the stock info
    var st = row.cells[8];
    if (!stock["stock"]) {
        stock["stock"] = {};
    }
    for (var rown=0; rown < st.firstChild.rows.length; rown++) {
        var aRow = st.firstChild.rows[rown];
        for (var celln=0; celln < aRow.cells.length; celln++) {
            var aCell = aRow.cells[celln];
            var com = aCell.getElementsByTagName ("img")[0].getAttribute ("alt");
            com = res_names[com];
            var amount = Number(aCell.textContent.substr (2));
            if (!stock["stock"][com]) {
                stock["stock"][com] = [0, 0, 0, 0, 0];
            }
            if (stock["stock"][com][1] != amount) {
                stock["stock"][com][1] = amount;
            }
        }
    }
    server.SaveFacility (tileId, stock, "stock");
}

FLogProcessOwnBuildings ();